package Dao;

import Entity.Officer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AdministratorDao {

    //在原文件中用第二个Officer替换第一个Officer
    public static void replace(Officer first,Officer second) throws IOException {
        ObjectMapper om = new ObjectMapper();

        File file = new File("files\\Officer.json");
        List<Officer> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Officer>>() {});
        }
        for (int i = 0, listSize = list.size(); i < listSize; i++) {
            Officer officer = list.get(i);
            if (officer.equals(first)) {
                list.remove(officer);
                list.add(second);
            }
        }
        om.writeValue(file,list);
    }

    //返回管理员
    public static Officer getAdmin() throws IOException {
        ObjectMapper om = new ObjectMapper();

        File file = new File("files\\Officer.json");
        List<Officer> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Officer>>() {});
        }
        for(Officer officer:list){
            if(officer.getJurisdiction()==2) return officer;
        }
        return null;
    }

    //返回普通职工
    public static List<Officer> getStaff() throws IOException {
        ObjectMapper om = new ObjectMapper();

        File file = new File("files\\Officer.json");
        List<Officer> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Officer>>() {});
        }

        Officer tmp = new Officer();
        for(Officer officer:list){
            if(officer.getJurisdiction()==2){
                tmp = officer;
                break;
            }
        }
        list.remove(tmp);
        return list;
    }

    //根据职称返回对应的Officer集合
    public static List<Officer> getOfficers(String type) throws IOException {
        ObjectMapper om = new ObjectMapper();

        File file = new File("files\\Officer.json");
        List<Officer> list = new ArrayList<>();
        List<Officer> officers = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Officer>>() {});
        }
        for(Officer officer:list){
            if(officer.getType().equals(type)) officers.add(officer);
        }
        return officers;
    }

    //移除该员工
    public static void remove(Officer officer) throws IOException {
        ObjectMapper om = new ObjectMapper();

        File file = new File("files\\Officer.json");
        List<Officer> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Officer>>() {});
        }

        list.remove(officer);
        om.writeValue(file,list);
    }

    //根据姓名返回对应的Officer
    public static Officer byname(String name) throws IOException {
        ObjectMapper om = new ObjectMapper();

        File file = new File("files\\Officer.json");
        List<Officer> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Officer>>() {});
        }
        for(Officer officer:list){
            if(officer.getName().equals(name)) return officer;
        }
        return null;
    }

    //根据Officer集合返回对应的姓名字符串集合
    public static List<String> getnames(List<Officer> list){
        List<String> namelist = new ArrayList<>();
        for (Officer officer:list){
            namelist.add(officer.getName());
        }
        return namelist;
    }
}
